package cn.stronglink.asset.message.mq.dam.handle;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.message.mq.dam.entity.IScreenCommandFactory;
import cn.stronglink.asset.message.mq.dam.entity.RackCodeEntity;
import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.asset.message.mq.dam.entity.dacai.UpdateArrayTextCommandFactory;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;
import cn.stronglink.asset.message.mq.topic.ReaderInfoSender;
import cn.stronglink.asset.module.dam.entity.dev.RackInfo;
import cn.stronglink.asset.module.dam.service.DamService;
import cn.stronglink.asset.module.dam.service.IDamService;
import cn.stronglink.suite.core.util.ContextUtils;

/**
 * 打开机柜界面时的处理
 * @author yuzhantao
 *
 */
public class OpenRackPageHandle extends AbstractScreenCommandHandle {
	protected final static String TOPIC_NAME = "serviceToDam";
	protected final static String ACTION_CODE = "screenRequest";
	protected final static String SEND_ACTION_CODE= "screenCommand";
	protected IDamService damService =(DamService)ContextUtils.getBean("sensorService");
	private ReaderInfoSender topicSender = (ReaderInfoSender) ContextUtils.getBean("readerInfoSender");
	private final static short RACK_TITLE_ID = 1;
	private final static short RACK_CONTENT_ID = 2;
	private final static short RACK_SCREEN_ID = 2;
	@Override
	protected String getCommandCode() {
		return "EEB11100000002100000";
	}
	
//	@Override
//	public boolean isHandle(MQMessageOfDAM msg) {
//		if(msg.getActioncode().equals(ACTION_CODE)) {
//			ScreenCommandEntity screenCommandEntity = ((JSONObject)msg.getAwsPostdata()).toJavaObject(ScreenCommandEntity.class);
//			if(screenCommandEntity.getCommand().equals("EEB11100000002100000FFFCFFFF")) {
//				return true;
//			}
//		}
//		return false;
//	}
	@Override
	protected void handleMessage(MQMessageOfDAM msg) {
		RackCodeEntity rackCodeEntity =  (RackCodeEntity) msg.getAwsPostdata();
		RackInfo rackInfo = damService.qryRackInfoByDevNo(rackCodeEntity.getRackConverCode());
		if(rackInfo!=null) {
			try {
				sendRackInfoToScreen(rackInfo,rackCodeEntity.getRackConverCode());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	private void sendRackInfoToScreen(RackInfo rackInfo,String rackConverCode) throws Exception {
		Map<Short,String> map = new HashMap<>();
		map.put(RACK_TITLE_ID, rackInfo.getRackName());
		StringBuffer sb = new StringBuffer();
		sb.append("机柜类型:"+(rackInfo.getRackTypeName()==null?"":rackInfo.getRackTypeName())+"\r\n");
		sb.append("机柜品牌:"+(rackInfo.getRackBrandName()==null?"":rackInfo.getRackBrandName())+"\r\n");
		sb.append("机柜型号:"+(rackInfo.getRackModelName()==null?"":rackInfo.getRackModelName())+"\r\n");
		sb.append("采集器编号:"+(rackInfo.getRackConverCode()==null?"":rackInfo.getRackConverCode())+"\r\n");
//		sb.append("机柜IP:"+rackInfo.getIp()+"\r\n");
		sb.append("机柜U高:"+rackInfo.getTotalU()+"\r\n");
		sb.append("资产数量:"+rackInfo.getAssetCount()+"\r\n");
		sb.append("左电源:"+(rackInfo.getLeftPower()==null?"":rackInfo.getLeftPower())+"\r\n");
		sb.append("右电源:"+(rackInfo.getRightPower()==null?"":rackInfo.getRightPower())+"\r\n");
		
		map.put(RACK_CONTENT_ID, sb.toString());
		IScreenCommandFactory rackScreenCommandFactroy = new UpdateArrayTextCommandFactory(rackConverCode, RACK_SCREEN_ID, map);
		ScreenCommandEntity sc =  rackScreenCommandFactroy.createScreenCommand();
		
		MQMessageOfDAM message = new MQMessageOfDAM();
		message.setActioncode(SEND_ACTION_CODE);
		message.setAwsPostdata(sc);
		message.setTimestamp(System.currentTimeMillis());
		
		String json = JSON.toJSONString(message);
		topicSender.send(TOPIC_NAME,json);
		logger.info("topic:{}  发送命令:{}",TOPIC_NAME,json);
	}
	

}
